package com.raincat.multimediapicker.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.raincat.multimediapicker.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Created by Administrator on 2018/3/23 0023.
 */

public class PickerGridAdapter extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater; // 视图容器

    private int imageWidth;
    private int mode = 0;
    private List<String> imageList = new ArrayList<>();

    public PickerGridAdapter(Context context, int imageWidth) {
        this.context = context;
        this.imageWidth = imageWidth;
        inflater = LayoutInflater.from(context);
    }

    public void setImagePathList(List<String> imageList) {
        this.imageList = imageList;
        mode = 0;
        notifyDataSetChanged();
    }

    public void setVideoPathList(List<String> videoList) {
        this.imageList = videoList;
        mode = 1;
        notifyDataSetChanged();
    }

    // 当图片已到选择阀值时，隐藏选择图片按钮
    public int getCount() {
        if (imageList.size() < ImageController.maxCount)
            return (imageList.size() + 1);
        else
            return imageList.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.picker_gridview_item, parent, false);
            holder = new ViewHolder();
            holder.image = (ImageView) convertView.findViewById(R.id.item_image);
            holder.deleteItem = (RelativeLayout) convertView.findViewById(R.id.item_delete);

            ViewGroup.LayoutParams ImageLayout = holder.image.getLayoutParams();
            ImageLayout.width = imageWidth;
            ImageLayout.height = imageWidth;
            holder.image.setLayoutParams(ImageLayout);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 放置标签
        holder.deleteItem.setTag(position);
        if (position == imageList.size()) {
            holder.deleteItem.setVisibility(View.GONE);
            Glide.with(context).load(R.mipmap.ic_addpic).dontAnimate().into(holder.image);
            if (position == ImageController.maxCount) {
                holder.image.setVisibility(View.GONE);
            }
        } else {
            holder.deleteItem.setVisibility(View.VISIBLE);
            Glide.with(context).asBitmap().load(imageList.get(position)).diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.image);
        }

        holder.deleteItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int count = (Integer) v.getTag();

                imageList.remove(count);
                if (mode == 0 && ImageController.realPathList.size() > count && ImageController.imageItemMap.get(ImageController.realPathList.get(count)) != null) {
                    ImageController.imageItemMap.remove(ImageController.realPathList.get(count));
                    if (ImageController.zoomPathList.size() == ImageController.realPathList.size())
                        ImageController.zoomPathList.remove(count);
                    ImageController.realPathList.remove(count);
                    ImageController.count -= 1;
                } else if (VideoController.realPathList.size() > count && VideoController.videoItemMap.get(VideoController.realPathList.get(count)) != null) {
                    VideoController.videoItemMap.remove(VideoController.realPathList.get(count));
                    VideoController.realPathList.remove(count);
                    VideoController.count -= 1;
                }

                notifyDataSetChanged();
            }
        });

        ((ViewGroup) convertView).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        return convertView;
    }

    public class ViewHolder {
        ImageView image;
        RelativeLayout deleteItem;
    }
}
